package com.lanou3g.mynewstest.module.news.pictures;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.google.gson.Gson;
import com.lanou3g.mynewstest.R;
import com.lanou3g.mynewstest.beans.NewsDetailBeans;
import com.lanou3g.mynewstest.beans.NewsPhotoSetBeans;
import com.lanou3g.mynewstest.beans.NewsPicturesBeans;
import com.lanou3g.mynewstest.module.news.headline.PhotoSetActivity;
import com.lanou3g.mynewstest.tools.MyRequestQueue;
import com.lanou3g.mynewstest.tools.StringRequestUtf;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 16/1/19.
 */
public class PicturesAdapter extends BaseAdapter {
    private Context context;
    private List<NewsPicturesBeans> data;


    public PicturesAdapter(Context context, List<NewsPicturesBeans> beans) {
        this.context = context;
        this.data = beans;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        MyThreePicViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_picture, parent, false);
            holder = new MyThreePicViewHolder(convertView);
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    goToPhotoSet(position);
                }
            });
            convertView.setTag(holder);
        } else {
            holder = (MyThreePicViewHolder) convertView.getTag();
        }

        holder.title.setText(data.get(position).getSetname());
        holder.replyCount.setText(data.get(position).getReplynum());
        final List<ImageView> imageViews = holder.imageViews;

        if (0 != data.get(position).getPics().size()) {
            for (int i = 0; i < 3; i++) {
                final int finalI = i;
                MyRequestQueue.getLoader().get(data.get(position).getPics().get(i), new ImageLoader.ImageListener() {
                    @Override
                    public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
                        Bitmap bitmap = response.getBitmap();
                        imageViews.get(finalI).setImageBitmap(bitmap);

                    }

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        error.printStackTrace();
                    }
                }, 300, 300);


            }

        }

        return convertView;
    }

    class MyThreePicViewHolder extends RecyclerView.ViewHolder {
        TextView title, replyCount;
        ImageView imageView1, imageView2, imageView3;
        List<ImageView> imageViews;

        public MyThreePicViewHolder(View itemView) {
            super(itemView);
            imageViews = new ArrayList<>();
            title = (TextView) itemView.findViewById(R.id.item_picture_threepic_title);
            replyCount = (TextView) itemView.findViewById(R.id.item_picture_threepic_replyCount);
            imageView1 = (ImageView) itemView.findViewById(R.id.item_picture_threepic_iv1);
            imageView2 = (ImageView) itemView.findViewById(R.id.item_picture_threepic_iv2);
            imageView3 = (ImageView) itemView.findViewById(R.id.item_picture_threepic_iv3);
            imageViews.add(imageView1);
            imageViews.add(imageView2);
            imageViews.add(imageView3);
        }
    }

    private void goToPhotoSet(int pos) {
        String url = data.get(pos).getSetid();

        url = "http://c.m.163.com/photo/api/set/0096/" + url + ".json";

        StringRequestUtf request = new StringRequestUtf(url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Gson gson = new Gson();
                        NewsPhotoSetBeans beans = gson.fromJson(response, NewsPhotoSetBeans.class);
                        PhotoSetActivity.goToPhotoSetActivity(context, beans);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                });
        MyRequestQueue.addRequest(request);
    }
}
